using System.Collections.Generic;
using System.Linq;

namespace Elderos.AI
{
    public class CategoricalFactor : Factor
    {
        public CategoricalFactor(string name) : base(name)
        {}

        private Dictionary<double, int> ValueOrder { get; set; }

        public void SetValues(IEnumerable<double> values)
        {
            ColumnNames = new List<string>();
            ValueOrder = new Dictionary<double, int>();

            var distinct = values.Distinct().ToList();
            distinct.Sort();
            for (int i = 0; i < distinct.Count; i++)
            {
                var value = distinct[i];
                ValueOrder.Add(value, i);
                ColumnNames.Add(Name + "_" + value);
            }
        }

        public override double[] GetSubvector(double logicalValue)
        {
            var result = new double[ValueOrder.Count];
            var number = ValueOrder[logicalValue];
            result[number] = 1;
            return result;
        }
    }
}